/*=============================================================================

  DIO-1616LN-FIT DIGITAL IO HAL MODULE

  FILE: ioHal_FIT.c
  
  DESCRIPTION

    DIO-1616LN-FIT Digital IO HAL Module

  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   07/17/04   nso4th    Initial version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <OBase.h>
#include <mxBase/dioDev.h>
#include <devSP/dev_mem.h>
#include "ioHal_FIT.h"
#include <stdlib.h>
#include <stdio.h>

#include <ccapdio.h>

/*=============================================================================
  ENUMERATORS
=============================================================================*/

/*=============================================================================
  MACRO DEFINITION
=============================================================================*/

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
  STRUCTURES
=============================================================================*/
typedef struct
{
	boolean		bInit;
	char			devName[100];
	char			dev[100];
	short		GroupID;
	short		UnitID;
	short		dwID;
	short		dwInNum;
	short		dwOutNum;
	dword		dwInVal;
	dword		dwOutVal;
	char			szErr[100];
	mcxt		pObj;
} FITSIO_s;

/*=============================================================================
  LOCALIZED FUCNTIONS
=============================================================================*/
static DioHal_s		*gpFITSIO = NULL;

// HAL Interface
static dword FITSIO_Execute(mcxt pDev, dword dwCmd, dword wParam, dword lParam);
static dword FITSIO_SetParam(mcxt pDev, dword dwType, dword wParam, dword lParam);
static dword FITSIO_GetParam(mcxt pDev, dword dwType, dword wParam, dword lParam);

// Allocation and Release
static mcxt FITSIO_Alloc(mcxt pObj, short devID);
static dword FITSIO_Free(FITSIO_s *pDev);
static dword FITSIO_Init(FITSIO_s *pDev);
static dword FITSIO_Close(FITSIO_s *pDev);

// IO's
dword FITSIO_Output(FITSIO_s *pDev, dword dwOut);
dword FITSIO_OutputBit(FITSIO_s *pDev, dword dwChan, boolean bOn);
dword FITSIO_OutputGet(FITSIO_s *pDev, dword *pOut);
dword FITSIO_OutputBitGet(FITSIO_s *pDev, dword dwChan, boolean *pOn);
dword FITSIO_Input(FITSIO_s *pDev, dword* pIn);
dword FITSIO_InputBit(FITSIO_s *pDev, dword dwChan, boolean *pOn);

// SYNC Task
//dword __stdcall FITSIO_Task(void *pCxt);

/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/
boolean DioHal_FITSIO_GetVersion(char *pszVer, int32 strLen)
{
	static char	szTmp[64];

	if(!pszVer)
	{
		return	FALSE;
	}
#ifdef _ODEBUG_
	sprintf(szTmp, "io_fitD.%s (%s)", "V03.001", __DATE__);
#else
	sprintf(szTmp, "io_fit.%s (%s)", "V03.001", __DATE__);
#endif
	if(strLen > (int32)strlen(szTmp))
	{
		strcpy(pszVer, szTmp);
		return	TRUE;
	}

	return	FALSE;
}

boolean DioHal_FITSIO_Register(void)
{
	DioHal_s		*pHal = gpFITSIO;

	if(pHal == NULL)
	{
		gpFITSIO = memCalloc(sizeof(*gpFITSIO));
		pHal = gpFITSIO;
	}

	pHal->dwDevID = DIODEV_FIT_SIO;
	pHal->pfnTaskCb = NULL;//FITSIO_Task;

	pHal->SetParamF = FITSIO_SetParam;
	pHal->GetParamF = FITSIO_GetParam;
	pHal->ExecuteF = FITSIO_Execute;

	return	DioDev_HalRegister(pHal);
}

boolean DioHal_FITSIO_Deregister(void)
{
	DioHal_s		*pHal = gpFITSIO;

	if(pHal)
	{
		DioDev_HalDeregister(pHal);
		memFree(pHal);
		pHal = NULL;
		gpFITSIO = NULL;

		return	TRUE;
	}
	
	return	FALSE;
}

/*=============================================================================
  HAL INTERFACING FUNCTIONS
=============================================================================*/
dword FITSIO_SetParam(mcxt pDev, dword dwType, dword wParam, dword lParam)
{
	FITSIO_s	*pSio = (FITSIO_s*)pDev;

	switch(dwType)
	{
	case DIO_OUT_CHAN_NUM_P:
	case DIO_IN_CHAN_NUM_P:
		return	FALSE;

	// CUSTOM COMMAND
	case DIO_HAL_BASE_P:
	default:
		break;
	}

	return	FALSE;
}

dword FITSIO_GetParam(mcxt pDev, dword dwType, dword wParam, dword lParam)
{
	FITSIO_s	*pSio = (FITSIO_s*)pDev;

	switch(dwType)
	{
	case DIO_OUT_CHAN_NUM_P:
	{
		if(wParam)
		{
			(dword)(*((dword*)wParam)) = pSio->dwOutNum;
		}
		return	pSio->dwOutNum;
	}
	case DIO_IN_CHAN_NUM_P:
	{
		if(wParam)
		{
			(dword)(*((dword*)wParam)) = pSio->dwInNum;
		}
		return	pSio->dwInNum;
	}
	// CUSTOM COMMAND
	case DIO_HAL_BASE_P:
	default:
		break;
	}
	return	FALSE;
}

dword FITSIO_Execute(mcxt pDev, dword dwCmd, dword wParam, dword lParam)
{
	FITSIO_s	*pSio = (FITSIO_s*)pDev;

	switch(dwCmd)
	{
	//--------------------------------------------
	// ALLOCATION AND RELEASE
	//--------------------------------------------
	case DIO_ALLOC_C:
		return	(dword)FITSIO_Alloc((mcxt)wParam, (short)lParam);

	case DIO_FREE_C:
		return	FITSIO_Free(pSio);

	case DIO_INIT_C:
		return	FITSIO_Init(pSio);

	case DIO_CLOSE_C:
		return	FITSIO_Close(pSio);

	//--------------------------------------------
	// IO
	//--------------------------------------------
	case DIO_OUTPUT_C: // wParam[in] = 32 Bit
		return	FITSIO_Output(pSio, lParam);
				
	case DIO_OUTBIT_C: // wParam[in] = Channel, lParam[in] = TRUE/FALSE
		return	FITSIO_OutputBit(pSio, wParam, (boolean)lParam);

	case DIO_OUTPUT_GET_C: // wParam[out] = 32 Bit Pointer
		return	FITSIO_OutputGet(pSio, (dword*)lParam);

	case DIO_OUTBIT_GET_C: // wParam[in] = Channel, lParam[out] = TRUE/FALSE (Pointer)
		return FITSIO_OutputBitGet(pSio, wParam, (boolean*)lParam);

	case DIO_INPUT_C: // wParam[out] = 32 bit pointer
		return FITSIO_Input(pSio, (dword*)lParam);

	case DIO_INPUTBIT_C: // wParam[in] = Channel, lParam[out] = TRUE/FALSE (Pointer)
		return	FITSIO_InputBit(pSio, wParam, (boolean*)lParam);
	
	//--------------------------------------------
	// CUST COMMAND
	//--------------------------------------------
	case DIO_HAL_BASE_C:
		break;

	default:
		break;
	}

	return	FALSE;
}

/*=============================================================================
  HARDWARE INTERFACING FUNCTIONS
=============================================================================*/
mcxt FITSIO_Alloc(mcxt pObj, short dwID)
{
	FITSIO_s		*pDev;

	pDev = memCalloc(sizeof(*pDev));
	pDev->pObj = pObj;
	pDev->dwID = dwID;

	return	pDev;
}

dword FITSIO_Free(FITSIO_s *pDev)
{
	if(pDev)
	{
		if(pDev->bInit)
		{
			FITSIO_Close(pDev);
		}
		
		memFree(pDev);
		return	TRUE;
	}

	return	FALSE;
}

dword FITSIO_Init(FITSIO_s *pDev)
{
	boolean			bSeq = FALSE;
	long				ret;
	short			index = 0;

	if(pDev->bInit)
		return	FALSE;

	// Initialize & getID
	ret = DioQueryDeviceName(index, &pDev->GroupID, &pDev->UnitID, &pDev->dwID, pDev->devName, pDev->dev);
	if(ret != DIO_ERR_SUCCESS)
	{
		DBGPRINT("[I/O] Get Device Name Failure !\n");
		return	FALSE;
	}

	
	ret = DioInit(pDev->devName, &pDev->dwID);
	if(ret != DIO_ERR_SUCCESS)
	{
		DBGPRINT("[I/O] Initialize Failure !\n");
		return	FALSE;
	}

	// Get maximum number of bits
	ret = DioGetMaxBits(pDev->dwID, &pDev->dwInNum, &pDev->dwOutNum);
	if(ret != DIO_ERR_SUCCESS)
	{
		DBGPRINT("[I/O] Bits Get number Failure !\n");
		return	FALSE;
	}
	bSeq = TRUE;
	
	if(bSeq == FALSE)
		return	FALSE;
	
	pDev->bInit = TRUE;

	return	TRUE;
}

dword FITSIO_Close(FITSIO_s *pDev)
{
	long				ret;
	
	if(!pDev->bInit)
		return	FALSE;

	ret = DioExit(pDev->dwID);
	if(ret != DIO_ERR_SUCCESS)
	{
		DBGPRINT("[I/O] Exit Failure !\n");
	}
	pDev->bInit = FALSE;

	return	TRUE;
}

//---------------------------------------------------------
// IO's
//---------------------------------------------------------
dword FITSIO_Output(FITSIO_s *pDev, dword dwOut)
{
	return	TRUE;
}

dword FITSIO_OutputBit(FITSIO_s *pDev, dword dwChan, boolean bOn)
{
	DioOutBit(pDev->dwID, (short)dwChan, (BOOL)bOn);
	return TRUE;
}

dword FITSIO_OutputGet(FITSIO_s *pDev, dword *pOut)
{
	return	TRUE;
}

dword FITSIO_OutputBitGet(FITSIO_s *pDev, dword dwChan, boolean *pOn)
{
	DioEchoBackBit(pDev->dwID, (short)dwChan, (BYTE*)pOn);
	return	(dword)pOn;

}

dword FITSIO_Input(FITSIO_s *pDev, dword* pIn)
{

	return	TRUE;
}

dword FITSIO_InputBit(FITSIO_s *pDev, dword dwChan, boolean *pOn)
{
	DioInpBit(pDev->dwID, (short)dwChan, (BYTE*)pOn);
	return	(dword)pOn;
}

/*--- END OF dioHal_ADAM5K.C ---*/
